package com.dkm.biz.gl.persistence.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.dkm.base.api.model.Page;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.base.db.mybatis.dao.MyBatisQueryDaoImpl;
import com.dkm.biz.gl.persistence.dao.GlCateQueryDao;
import com.dkm.biz.gl.persistence.entity.GlCatePo;

/**
 * 对象功能:总分类表。用于显示树层次结构的分类，可以允许任何层次结构。 QueryDao接口的实现
 * 开发公司:唯心六艺软件 - Craft6.cn
 * 开发人员:winston
 * 创建时间:2014-07-22 11:07:05
 */
@Repository
public class GlCateQueryDaoImpl extends MyBatisQueryDaoImpl<String, GlCatePo> implements GlCateQueryDao{

    @Override
    public String getNamespace() {
        return GlCatePo.class.getName();
    }

	public List<GlCatePo> findByTypeKey(String typeKey) {
		return findByKey("findByTypeKey", b().a("typeKey",typeKey).p());
	}

	public List<GlCatePo> findByTypeKeyAndDepth(String typeKey, int depth) {
		return findByKey("findByTypeKeyAndDepth", b().a("typeKey", typeKey).a("depth", depth).p());
	}

	public GlCatePo getByCateKey(String typeKey,String key) {
        return getByKey("getByCateKey",b().a("typeKey", typeKey).a("key", key).p());
	}

	public List<GlCatePo> findByCateKey(String key,Integer stat,String bizType,String seoType) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("key", key);
		params.put("stat", stat);
		if(StringUtils.isNotEmpty(bizType)){
			params.put("bizType", bizType);
		}
		if(StringUtils.isEmpty(seoType)){
			params.put("seoType", "web");
		}else{
			params.put("seoType", seoType);
		}
		return findByKey("getByKey", params);
	}

	public List<GlCatePo> findByCateName(String typeKey, String name) {
		return findByKey("findByCateName",b().a("typeKey", typeKey).a("name", name).p());
	}

	public List<GlCatePo> findByTypeKeyAndParentId(String typeKey,
			String parentId, Integer stat,String bizType,String seoType) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("typeKey", typeKey);
		params.put("parentId", parentId);
		params.put("stat", stat);
		if(StringUtils.isNotEmpty(bizType)){
			params.put("bizType", bizType);
		}
		if(StringUtils.isEmpty(seoType)){
			params.put("seoType", "web");
		}else{
			params.put("seoType", seoType);
		}
		return findByKey("findByTypeKeyAndParentId", params);
	}

	@Override
	public GlCatePo getCatePoByChild(String key) {
		
		return getByKey("getCatePoByChild", b().a("key", key).p());
	}

	@Override
	public List<GlCatePo> findAllPaged(Map<String, Object> buildMap, Page page) {
		// TODO Auto-generated method stub
		return findByKey("findAll",buildMap,page);
	}

	@Override
	public List<GlCatePo> findIdByParentName(String qtTypeId) {
		Map<String, Object> params = b().a("qtTypeId", qtTypeId).p();
		return findByKey("findIdByParentName", params);
	}

	@Override
	public List<GlCatePo> getSiteMapData() {
		return findByKey("getSiteMapData", null);
	}

	@Override
	public List<GlCatePo> findCateSyncList(Page page) {
		return findByKey("findCateSyncList",null,page);
	}

	@Override
	public int findCateSyncByParentId(String pid) {
		String statement = getNamespace() + ".findCateSyncByParentId";
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("pid", pid);
		Object object = sqlSessionTemplate.selectOne(statement,params);
		return (Integer)object;
	}

	@Override
	public List findCateSyncByPath(String[] ids) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		List list=findByKey("findCateSyncByPath",params);
		return list;
	}

	@Override
	public int findCateSyncListCount() {
		String statement = getNamespace() + ".findCateSyncListCount";
		return (Integer)sqlSessionTemplate.selectOne(statement);
	}
	@Override
	public List<GlCatePo> findByIdsOrder(List<String> ids){
		if(ids.size()>0){
			Map<String, Object> map = b().a("ids", ids).p();
			return sqlSessionTemplate.selectList(getNamespace() + ".findByIdsOrder", map);
		}
		return new ArrayList<GlCatePo>();
	}

	@Override
	public List<GlCatePo> findGlCateByDepthAndTypeKey(String depth, String typeKey) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("depth", depth);
		params.put("typeKey", typeKey);
		return findByKey("findGlCateByDepthAndTypeKey", params);
	}

	@Override
	public List<GlCatePo> findCateByPath(String path,String tag) {
		Map<String, Object> params=new HashMap<String, Object>();
		StringBuffer sbBuffer=new StringBuffer();
		if(StringUtils.isNotEmpty(path)){
			sbBuffer.append("AND path_ LIKE '").append(path).append("%'");
		}
		if(StringUtils.isNotEmpty(tag)){
			sbBuffer.append("AND tag_ = '").append(tag).append("' ORDER BY depth_ DESC");
		}
		params.put("whereSql", sbBuffer.toString());
		return findByKey("findCateByPath", params);
	}
}
